package com.stepin2it.newstalk;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NewsListActivity extends Activity
{
	private static final String TAG = "NewsListActivity";

	private static final boolean DEBUG = true;
	
	private String[] mListOfNames = new String[] { "John", "Paul", "George" };

	private Photos mPhotos = new Photos();
	
	
	private List<Photo> mListOfPhotos = new ArrayList<Photo>();

	private FlickrPhotoResponse mFlickrPhotoResponse = null;

	private ListView listView1;
	
	public class MyCustomAdapter extends ArrayAdapter<Photo>
	{
		public MyCustomAdapter(Context context, int textViewResourceId,	List<Photo> mListOfPhotos)
		{
			super(context, textViewResourceId, mListOfPhotos);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			
			final int rownumber = position;
			View row = convertView;

			if (row == null)
			{
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.list_item, parent, false);
			}
			
			row.setOnClickListener(new View.OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent(NewsListActivity.this, NewsDetailViewActivity.class);
					
					intent.putExtra("PHOTO_ID", mListOfPhotos.get(rownumber).getId() );
					Log.d(TAG, "" + mListOfPhotos.get(rownumber).getId() );
					startActivity(intent);
				}
				
			});

			TextView list_name = (TextView) row.findViewById(R.id.list_name);
			list_name.setText(mListOfPhotos.get(position).getTitle());
			
			/*
			TextView list_tagline = (TextView) row
					.findViewById(R.id.list_tagline);

			

			list_tagline.setText(mListOfPhotos.get(position).getDescription().getContent());
			
			int i = 0;

			imageView1 = (ImageView) row.findViewById(R.id.imageView1);
			imageView1.setImageResource(R.drawable.ic_launcher);
			
			if(mPhotoBitmap.get(position) != null){
				if(mPhotoBitmap.get(position) != null){
					imageView1.setImageBitmap(mPhotoBitmap.get(position));
				}
				else{
					Log.d(TAG, "Bitmap returned NULL");
				}
			}
			*/
			
			return row;
			

		}
	}


	private class BackgroundTask extends AsyncTask<URL, Integer, Long>
	{
		@Override
		public Long doInBackground(URL... urls)
		{
			try
			{
				read();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			return null;
		}

		protected void onProgressUpdate(Integer... progress)
		{

		}
		@Override
		public void onPreExecute()
		{

		}
		@Override
		public void onPostExecute(Long result)
		{
			display();
		}
	}

	protected void read() throws IOException
	{
		mFlickrPhotoResponse = DataManager
				.readFlickrPhotoResponse(FeaturesConfiguration.flickrSearchUrl);
		mPhotos = mFlickrPhotoResponse.getPhotos();
		
		mListOfPhotos = mPhotos.getFlickrPhotoList();
	}
	protected void display()
	{
		MyCustomAdapter ad = new MyCustomAdapter(this, 0, mListOfPhotos);
		

		listView1.setAdapter(ad);
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		FeaturesConfiguration f = new FeaturesConfiguration();
		Log.d(TAG, f.flickrSearchUrl);
		// if (DEBUG) Log.d(TAG, "onCreate was called");
		int activitynumber = this.getIntent().getIntExtra("ACTIVITY_NUMBER", 0);
		Log.d(TAG, "The value of ACTIVIY_NUMBER is : " + activitynumber);
		setContentView(R.layout.activity_newslist);
		listView1 = (ListView) this.findViewById(R.id.listView1);

		

		// call the DataManager method for parsing the photo response
		new BackgroundTask().execute();

	}

	@Override
	public void onStart()
	{
		super.onStart();
		Log.d(TAG, "onStart was called");

	}

	@Override
	public void onResume()
	{
		super.onResume();
		Log.d(TAG, "onResume was called");

	}

	@Override
	public void onPause()
	{
		super.onPause();
		Log.d(TAG, "onPause was called");

	}

	@Override
	public void onStop()
	{
		super.onStop();
		Log.d(TAG, "onStop was called");
	}

	@Override
	public void onDestroy()
	{
		super.onDestroy();
		Log.d(TAG, "onDestroy was called");
	}

}
